package com.spring.controller.dto.databind;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenhs on 2018/6/21.
 */
public class UserMap {
    private Map<String, User> users;

    public Map<String, User> getUsers() {
        return users;
    }

    public void setUsers(Map<String, User> users) {
        this.users = users;
    }


    public static void main(String[] args) {
        User user1 = new User();
        user1.setName("China");
        user1.setAge(100);
        Address address1 = new Address();
        address1.setId(1);
        address1.setName("Beijing");
        user1.setAddress(address1);

        User user2 = new User();
        user2.setName("America");
        user2.setAge(110);
        Address address2 = new Address();
        address2.setId(2);
        address2.setName("NewYork");
        user2.setAddress(address2);

        Map<String, User> users = new HashMap();
        users.put("a", user1);
        users.put("b", user2);

        SerializeConfig mapping = new SerializeConfig();
        String jsonStr = JSON.toJSONString(users, mapping);
        System.out.println(jsonStr);

        UserMap userMap = new UserMap();
        userMap.setUsers(users);
        String jsonStr2 = JSON.toJSONString(userMap, mapping);
        System.out.println(jsonStr2);



    }

}